const http = require('../../../utils/http.js'); //相对路径
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navindex: 0,
    checked: false,
    bianji: false,
    weiduList: [],
    yiduList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.noticeData){
      this.setData({
        weiduList:options.noticeData
      })
    }
  },
  onShow() {
    http.postRequest('/mini/v1/notice/unread_list',{},res=>{
      for(let i in res.data.list) {
        res.data.list[i].date = http.transTime(res.data.list[i].time);
        res.data.list[i].title = res.data.list[i].msg.substring(0,4);
      }
      this.setData({
        weiduList: res.data.list
      })
    })
  },
  bianji() {
    let bianji = this.data.bianji
    bianji = !bianji
    this.setData({
      bianji: bianji
    })
  },
  gopage(e) {
    wx.navigateTo({
      url: '/pages/index/work/work?name=' + 1 + '&time=' + e.currentTarget.dataset.time,
    })
  },
  navindex(e) {
    let index = e.currentTarget.dataset.index
    let weiduList = this.data.weiduList;
    let yiduList = this.data.yiduList;
    for(let item of weiduList) {
      item.checked = false
    }
    for(let item of yiduList) {
      item.checked = false
    }
    if(e.currentTarget.dataset.index == 0) {
      http.postRequest('/mini/v1/notice/unread_list',{},res=>{
        for(let i in res.data.list) {
          res.data.list[i].date = http.transTime(res.data.list[i].time);
          res.data.list[i].title = res.data.list[i].msg.substring(0,4);
        }
        this.setData({
          weiduList: res.data.list
        })
      })
    } else {
      http.postRequest('/mini/v1/notice/read_list',{},res=> {
        for(let i in res.data) {
          res.data[i].date = http.transTime(res.data[i].time);
          res.data[i].title = res.data[i].msg.substring(0,4);
          res.data[i].checked = false
        }
        this.setData({
          yiduList: res.data
        })
      })
    }
    this.setData({
      navindex: index,
      checked: false,
      weiduList: weiduList,
      yiduList: yiduList,
      bianji: false
    })
  },
  checked1(e){
    let id = e.currentTarget.dataset.id;
    let yiduList = this.data.yiduList;
    yiduList[id].checked = !yiduList[id].checked;
    this.setData({
      yiduList: yiduList
    })
  },
  setunread() {
    let time_array = [];
    let yiduList = this.data.yiduList;
    for(let i in yiduList) {
      if(yiduList[i].checked) {
        time_array.push(yiduList[i].time);
      }
    }
    http.postRequest('/mini/v1/notice/set_unread',{time_array: time_array},res=> {
      if(res.code == 0) {
        wx.showToast({
          title: res.msg,
          icon: 'success'
        })
        for(let i in yiduList) {
          if(yiduList[i].checked) {
            yiduList.splice(i,1);
          }
        }
        this.setData({
          yiduList: yiduList,
          bianji: false
        })
      }
    })
  },
  checked2() {
    if(this.data.navindex == 0) {
      let weiduList = this.data.weiduList;
      for(let item of weiduList) {
        item.checked = !item.checked
      }
      let checked = this.data.checked;
      checked = !checked
      this.setData({
        weiduList: weiduList,
        checked: checked
      })
    }else if(this.data.navindex == 1) {
      let yiduList = this.data.yiduList;
      for(let item of yiduList) {
        item.checked = !item.checked
      }
      let checked = this.data.checked;
      checked = !checked
      this.setData({
        yiduList: yiduList,
        checked: checked
      })
    }
  }
})